prepare("select market, town, market_section, zone, line_no,stall_no, stall_type from stall_details") ; //$SQL = "SELECT sn, studentname, regno from students"; $header = ''; $result =''; //$exportData = mysql_query ($SQL ) or die ( "Sql error : " . mysql_error( ) ); $sql->execute() ; $fields = $sql->rowcount() ; //for ( $i = 0; $i < $fields; $i++ ) //{ //$header .= mysql_field_name( $exportData , $i ) . "\t"; //} while( $row = $sql->fetch(pdo::FETCH_ASSOC) ) { $line = ''; foreach( $row as $value ) { if ( ( !isset( $value ) ) || ( $value == "" ) ) { $value = "\t"; } else { $value = str_replace( '"' , '""' , $value ); $value = '"' . $value . '"' . "\t"; } $line .= $value; } $result .= trim( $line ) . "\n"; } $result = str_replace( "\r" , "" , $result ); if ( $result == "" ) { $result = "\nNo Record(s) Found!\n"; } header("Content-type: application/octet-stream"); header("Content-Disposition: attachment; filename=stalls.xls"); header("Pragma: no-cache"); header("Expires: 0"); print "$header\n$result"; ?>